/*
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
 * provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "It_posix_container.h"
using namespace std;
#define STACK_SIZE (1024 * 1024)    /* Stack size for cloned child */
#define CHILD_FUNC_ARG (0x2088)

static int childFunc(void *arg) {
    int value = *((int*)arg);
    printf("arg=%#x in childFunc\n", value);
    ICUNIT_ASSERT_EQUAL(value, CHILD_FUNC_ARG, value);
    sleep(3);
    return 2;
}

static UINT32 Testcase(VOID)
{
    int ret;
    char *stack;
    char *stackTop;
    stack = (char*)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
    stackTop = stack + STACK_SIZE;

    printf("childFunc=%#x, stackTop=%#x, flag=%#x\n", (void*)childFunc, (void*)stackTop, SIGCHLD);
    int arg = CHILD_FUNC_ARG;

    int flags = CLONE_NEWNS | CLONE_NEWUTS | SIGCHLD;
    auto pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "1.child pid is " << pid << endl;
    int status;
    ret = waitpid(pid, &status, 0);
    cout << "1.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWNS | CLONE_NEWUSER | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "2.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "2.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWNS  | CLONE_NEWPID | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "3.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "3.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWNS  | CLONE_NEWNET | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "4.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "4.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWUTS | CLONE_NEWUSER | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "5.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "5.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWUTS | CLONE_NEWPID | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "6.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "6.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWUTS | CLONE_NEWNET | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "7.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "7.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWUSER | CLONE_NEWPID | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "8.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "8.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWUSER | CLONE_NEWNET | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "9.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "9.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWPID | CLONE_NEWNET | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "10.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "10.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWNS  | CLONE_NEWUTS | CLONE_NEWUSER | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "11.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "11.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWNS  | CLONE_NEWUTS | CLONE_NEWUSER | CLONE_NEWPID | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "12.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "12.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    flags = CLONE_NEWNS  | CLONE_NEWUTS | CLONE_NEWUSER | CLONE_NEWPID | CLONE_NEWNET | SIGCHLD;
    pid = clone(childFunc, stackTop, flags, &arg);
    ICUNIT_ASSERT_NOT_EQUAL(pid, -1, pid);
    cout << "13.child pid is " << pid << endl;
    ret = waitpid(pid, &status, 0);
    cout << "13.return value of waitpid is " << ret << endl;
    ICUNIT_ASSERT_EQUAL(ret, pid, ret);


    ret = WIFEXITED(status);
    ICUNIT_ASSERT_NOT_EQUAL(ret, 0, ret);

    int exitCode = WEXITSTATUS(status);
    cout << "exit code of child process is " << exitCode << endl;
    ICUNIT_ASSERT_EQUAL(exitCode, 2, exitCode);
    return 0;
}

VOID ItPosixContainer001FLAGS(void)
{
    TEST_ADD_CASE("IT_POSIX_CONTAINER_001_FLAGS", Testcase, TEST_POSIX, TEST_CONTAINER, TEST_LEVEL0, TEST_FUNCTION);
}

